package com.dou.you.che.web.controller.sys;

import com.dou.you.che.web.controller.base.BaseAct;
import com.dou.you.che.comm.annotation.SysLogI;
import com.dou.you.che.comm.utils.page.PageUtils;
import com.dou.you.che.comm.utils.query.QueryParam;
import com.dou.you.che.comm.utils.response.RspData;
import com.dou.you.che.comm.utils.validator.ValidatorUtils;
import com.dou.you.che.core.entity.sys.SysScheduleJob;
import com.dou.you.che.service.sys.SysScheduleJobServiceI;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 定时任务
 * @Author 黄国刚【1058118328@qq.com】
 */
@RestController
@RequestMapping("sys/schedule")
public class SysScheduleAct extends BaseAct {

    @Autowired
    private SysScheduleJobServiceI scheduleJobService;

    /**
     * 定时任务列表
     */
    @SysLogI("定时任务列表")
    @RequestMapping("/list")
    @RequiresPermissions("sys:schedule:list")
    public RspData list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        QueryParam query = new QueryParam(params);
        List<SysScheduleJob> jobList = scheduleJobService.queryList(query);
        int total = scheduleJobService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(jobList, total, query.getLimit(), query.getPage());

        return RspData.ok().put("page", pageUtil);
    }

    /**
     * 定时任务信息
     */
    @SysLogI("定时任务信息")
    @RequestMapping("/info/{jobId}")
    @RequiresPermissions("sys:schedule:info")
    public RspData info(@PathVariable("jobId") Long jobId) {
        SysScheduleJob schedule = scheduleJobService.queryObject(jobId);

        return RspData.ok().put("schedule", schedule);
    }

    /**
     * 保存定时任务
     */
    @SysLogI("保存定时任务")
    @RequestMapping("/save")
    @RequiresPermissions("sys:schedule:save")
    public RspData save(@RequestBody SysScheduleJob scheduleJob) {
        if(isTest){
            return RspData.error("演示模式不允许操作");
        }
        ValidatorUtils.validateEntity(scheduleJob);

        return scheduleJobService.save(scheduleJob);
    }

    /**
     * 修改定时任务
     */
    @SysLogI("修改定时任务")
    @RequestMapping("/update")
    @RequiresPermissions("sys:schedule:update")
    public RspData update(@RequestBody SysScheduleJob scheduleJob) {
        if(isTest){
            return RspData.error("演示模式不允许操作");
        }
        ValidatorUtils.validateEntity(scheduleJob);

        return scheduleJobService.update(scheduleJob);
    }

    /**
     * 删除定时任务
     */
    @SysLogI("删除定时任务")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:schedule:delete")
    public RspData delete(@RequestBody Long[] jobIds) {
        if(isTest){
            return RspData.error("演示模式不允许操作");
        }
        scheduleJobService.deleteBatch(jobIds);

        return RspData.ok();
    }

    /**
     * 立即执行任务
     */
    @SysLogI("立即执行任务")
    @RequestMapping("/run")
    @RequiresPermissions("sys:schedule:run")
    public RspData run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);

        return RspData.ok();
    }

    /**
     * 暂停定时任务
     */
    @SysLogI("暂停定时任务")
    @RequestMapping("/pause")
    @RequiresPermissions("sys:schedule:pause")
    public RspData pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);

        return RspData.ok();
    }

    /**
     * 恢复定时任务
     */
    @SysLogI("恢复定时任务")
    @RequestMapping("/resume")
    @RequiresPermissions("sys:schedule:resume")
    public RspData resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);

        return RspData.ok();
    }
}
